package com.qiyun.dinghuohui.dialog;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qiyun.dinghuohui.R;
import com.qiyun.dinghuohui.base.BaseDialog;
import com.qiyun.dinghuohui.common.ApiName;
import com.qiyun.dinghuohui.common.Preferences;
import com.qiyun.dinghuohui.http.HttpUtils;
import com.qiyun.dinghuohui.utils.CommonUtil;
import com.qiyun.dinghuohui.utils.GsonUtil;
import com.qiyun.dinghuohui.utils.Inject;
import com.qiyun.dinghuohui.utils.LogDebug;
import com.qiyun.dinghuohui.utils.PreferenceUtil;
import com.qiyun.dinghuohui.utils.RotateUtils;
import com.qiyun.dinghuohui.vo.ServersBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xlm on 2018\5\26 0026
 */

public class ServerSetDialog extends BaseDialog {

    @Inject(R.id.dialog_choose)
    private TextView mTxtChoose;//选择
    @Inject(R.id.dialog_eidt)
    private TextView mTxtEdit;//输入
    @Inject(R.id.cancle)
    private TextView cancle;//取消
    @Inject(R.id.confirm)
    private TextView confirm;//确认
    @Inject(R.id.dialog_choose_server)
    private AutoCompleteTextView mEditChooseServer;//选择输入框server
    @Inject(R.id.dialog_choose_name)
    private AutoCompleteTextView mEditChooseName;//选择输入框name
    @Inject(R.id.dialog_choose_icon_server)
    private ImageView mEditChooseServerIcon;//选择输入框server图标
    @Inject(R.id.dialog_choose_icon_name)
    private ImageView mEditChooseNameIcon;//选择输入框name图标
    @Inject(R.id.dialog_edit_code)
    private AutoCompleteTextView mEditEditServer;//编辑输入框server
    @Inject(R.id.dialog_edit_name)
    private AutoCompleteTextView mEditEditName;//编辑输入框name
    @Inject(R.id.ll_dialog_choose)
    private LinearLayout mllDialogChoose;//选择模块
    @Inject(R.id.ll_dialog_edit)
    private LinearLayout mllDialogEdit;//输入模块
    @Inject(R.id.server_eddit)
    private EditText mEdit;

    private String server;
    private String name;
    private String code;
    private ServersBean serversBean;

    private static boolean isChooseSelect = true;

    private static boolean isChooseServerIconSelect = false;
    private static boolean isChooseNameIconSelect = false;

    public ServerSetDialog(Context context) {
        super(context);

    }

    public ServerSetDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected ServerSetDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.dialog_server_set);
    }

    @Override
    protected void initViews() {
        initWindow(0.85f, 0);
        setAnimate(R.style.Animatedialog3);
    }

    @Override
    protected void onSetListener() {
        mTxtChoose.setOnClickListener(this);
        mTxtEdit.setOnClickListener(this);

        cancle.setOnClickListener(this);
        confirm.setOnClickListener(this);

        mEditChooseServerIcon.setOnClickListener(this);
        mEditChooseNameIcon.setOnClickListener(this);
    }

    public void setServerData(ServersBean serversBean) {
        this.serversBean = serversBean;
        setConfigData(serversBean);
    }

    private void setConfigData(ServersBean serversBean) {
        if (serversBean != null) {
            List<String> servers = new ArrayList<>();
            List<String> names = new ArrayList<>();
            for (ServersBean.ListBean bean : serversBean.getList()) {
                servers.add(bean.getIp());
                names.add(bean.getName());
            }
            setChooseSelect();
            setServerChoose(servers);
            setServerChooseName(names);
            setCodeAndName(names);
        }
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        if (serversBean != null) {
            setConfigData(serversBean);
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.dialog_choose:
                setChooseSelect();
                break;
            case R.id.dialog_eidt:
                setEditSelect();
                break;
            case R.id.cancle:
                dismiss();
                break;
            case R.id.confirm:
                if (isChooseSelect) {
                    server = mEditChooseServer.getText().toString();
                    name = mEditChooseName.getText().toString();
                    listener.onConfirm(server, name, null,true);
                } else {
                    code = mEditEditServer.getText().toString();
                    name = mEditEditName.getText().toString();
                    listener.onConfirm(null, name, code,false);
                }
                break;
            case R.id.dialog_choose_icon_server:
                onClickServerIcon();
                break;
            case R.id.dialog_choose_icon_name:
                onClickNameIcon();
                break;
        }
    }

    /**
     * 设置服务器地址
     */
    private void setServerChoose(final List<String> servers) {
        if (mEditChooseServer != null) {
            String ip = (String) PreferenceUtil.getParam(Preferences.SERVER_URL, "");
            if (!CommonUtil.isEmpty(ip)) {
                mEditChooseServer.setText(ip.substring(7,ip.length()));
            } else {
                mEditChooseServer.setText(servers.get(0));
            }
            mEditChooseServer.clearFocus();
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getThis(),
                    android.R.layout.simple_dropdown_item_1line, servers);
            mEditChooseServer.setAdapter(adapter);
            mEditChooseServer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    mEditChooseServer.setText(((TextView) view).getText().toString());
                    String content = mEditChooseServer.getText().toString();
                    if (!CommonUtil.isEmpty(content)) {
                        mEditChooseServer.setSelection(content.length());
                    }
                }
            });
        }
    }

    /**
     * 设置公司名字下拉列表
     *
     * @param names
     */
    private void setServerChooseName(final List<String> names) {
        if (mEditChooseName != null) {
            String name = (String) PreferenceUtil.getParam(Preferences.COMPANY_NAME, "");
            if (!CommonUtil.isEmpty(name)) {
                mEditChooseName.setText(name);
            } else {
                mEditChooseName.setText(names.get(0));
            }
            mEditChooseName.clearFocus();
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getThis(),
                    android.R.layout.simple_dropdown_item_1line, names);
            mEditChooseName.setAdapter(adapter);
            mEditChooseName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    mEditChooseName.setText(((TextView) view).getText().toString());
                    String content = mEditChooseName.getText().toString();
                    if (!CommonUtil.isEmpty(content)) {
                        mEditChooseName.setSelection(content.length());
                    }
                }
            });
        }
    }

    private void setCodeAndName(final List<String> names) {
        if (mEditEditServer != null) {
            String code = (String) PreferenceUtil.getParam(Preferences.COMPANY_CODE, "");
            String name = (String) PreferenceUtil.getParam(Preferences.COMPANY_NAME, "");
            if (!CommonUtil.isEmpty(code)) {
                mEditEditServer.setText(code);
            }
            if (!CommonUtil.isEmpty(name)) {
                mEditEditName.setText(name);
            } else {
                mEditEditName.setText(names.get(0));
            }
            mEditEditServer.clearFocus();
            mEditEditName.clearFocus();
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getThis(),
                    android.R.layout.simple_dropdown_item_1line, names);
            mEditEditName.setAdapter(adapter);
            mEditEditName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    mEditEditName.setText(((TextView) view).getText().toString());
                    String content = mEditEditName.getText().toString();
                    if (!CommonUtil.isEmpty(content)) {
                        mEditEditName.setSelection(content.length());
                    }
                }
            });
        }
    }

    private class MyServerAdapter extends ArrayAdapter<String> {
        private List<String> objects;
        private Context context;

        public MyServerAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<String> objects) {
            super(context, resource, objects);
            this.context = context;
            this.objects = objects;
        }

        @Override
        public int getCount() {
            return objects.size();
        }

        @Override
        public String getItem(int position) {
            return objects.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                LayoutInflater mInflater = LayoutInflater.from(context);
                convertView = mInflater.inflate(R.layout.item_server_name, null);
                viewHolder.content = (TextView) convertView.findViewById(R.id.content);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.content.setText(objects.get(position));
            return convertView;
        }

        class ViewHolder {
            TextView content;
        }

    }


    /**
     * 设置选择
     */
    private void setChooseSelect() {
        isChooseSelect = true;
        mTxtChoose.setTextColor(getThis().getResources().getColor(R.color.colorPrimaryDark));
        mTxtEdit.setTextColor(getThis().getResources().getColor(R.color.login_account));
        mllDialogChoose.setVisibility(View.VISIBLE);
        mllDialogEdit.setVisibility(View.GONE);
    }

    /**
     * 设置输入
     */
    private void setEditSelect() {
        isChooseSelect = false;
        mTxtEdit.setTextColor(getThis().getResources().getColor(R.color.colorPrimaryDark));
        mTxtChoose.setTextColor(getThis().getResources().getColor(R.color.login_account));
        mllDialogEdit.setVisibility(View.VISIBLE);
        mllDialogChoose.setVisibility(View.GONE);
    }

    /**
     * 点击server图标
     */
    private void onClickServerIcon() {
        if (!isChooseServerIconSelect) {
            isChooseServerIconSelect = true;
//            RotateUtils.rotateArrowTop(mEditChooseServerIcon);
        } else {
            isChooseServerIconSelect = false;
            // RotateUtils.rotateArrowBottom(mEditChooseServerIcon);
        }
    }

    /**
     * 点击name图标
     */
    private void onClickNameIcon() {
        if (!isChooseNameIconSelect) {
            isChooseNameIconSelect = true;
//            RotateUtils.rotateArrowTop(mEditChooseNameIcon);
        } else {
            isChooseNameIconSelect = false;
            // RotateUtils.rotateArrowBottom(mEditChooseNameIcon);
        }
    }

    private Listener listener;

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    public static class Listener {
        public void onConfirm(String server, String name, String code,boolean isChooose) {

        }
    }
}
